package dao;

import java.io.Serializable;

import javax.persistence.PersistenceException;

import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;
@SuppressWarnings("serial")
public class AbstractDAO implements Serializable{
	public void saveOrUpdate(Object object) throws Exception{
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.saveOrUpdate(object);
			transaction.commit();

		} catch (PersistenceException e) {
			transaction.rollback();
			throw new Exception(e.getMessage());
		}finally{
			session.close();
		}
	}
	public void delete(Object object) throws Exception{
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.delete(object);
			transaction.commit();
		} catch (PersistenceException e) {
			transaction.rollback();
			throw new Exception(e.getMessage());
		}finally{
			session.close();
		}
	}
	public void update(Object object) throws Exception{
		Session session = null;
		Transaction transaction = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			session.update(object);
			transaction.commit();
		} catch (PersistenceException e) {
			transaction.rollback();
			throw new Exception("Erro ao Salvar ' ABSTRACT DAO'");
		}finally{
			session.close();
		}
	}
}
